#include "uti_timer.h"
#include <atltime.h>

//
//CTimer::CTimer(unsigned int interval,   // interval in ms
//	unsigned int duetime,// true to call first event immediately
//	bool once)       // true to call timed event only once
//{
//	m_hTimer = NULL;
//	m_hTimerStop = NULL;
//
//	m_hTimerQueue = NULL;
//	m_hTimerQueuestop = NULL;
//
//
//	if (false == Start(interval, duetime, once))
//	{
//		printf("[CTimer] Start Failed\n");
//	}
//
//}
//
//CTimer::~CTimer()
//{
//	if (false == Stop())
//	{
//		printf("[CTimer] Stop Failed\n");
//	}
//}
//
//
//bool CTimer::Start(unsigned int interval,   // interval in ms
//	unsigned int duetime,// true to call first event immediately
//	bool once)       // true to call timed event only once
//{
//	if( m_hTimer || m_hTimerStop || m_hTimerQueue || m_hTimerQueuestop)
//	{
//		printf("[Start] Not NULL\n");
//		return false;
//	}
//
//	m_hTimerQueuestop = CreateEvent(NULL, FALSE, FALSE, NULL);
//	if (NULL == m_hTimerQueuestop)
//	{
//		printf("[Start] m_hTimerQueuestop failed (%d)\n", GetLastError());
//		return false;
//	}
//
//	// Create the timer queue.
//	m_hTimerQueue = CreateTimerQueue();
//	if (NULL == m_hTimerQueue)
//	{
//		printf("[Start] m_hTimerQueue failed (%d)\n", GetLastError());
//		return false;
//	}
//
//	m_hTimerStop = CreateEvent(NULL, FALSE, FALSE, NULL);
//	if (NULL == m_hTimerStop)
//	{
//		printf("[Start] m_hTimerStop failed (%d)\n", GetLastError());
//		return false;
//	}
//
//
//	BOOL success = CreateTimerQueueTimer( &m_hTimer,
//		NULL,
//		CTimerRoutine::TimerRoutine,
//		NULL,
//		duetime,
//		once ? 0 : interval,
//		//WT_EXECUTEDEFAULT);
//		WT_EXECUTEINTIMERTHREAD);
//
//	return( success != 0 );
//}
//
//bool CTimer::Stop()
//{
//	if (m_hTimer && m_hTimerStop && m_hTimerQueue && m_hTimerQueuestop)
//	{
//		//CTimeCounter tc;
//		//tc.Start();
//		if (false == DeleteTimerQueueTimer(m_hTimerQueue, m_hTimer, m_hTimerStop) && ERROR_IO_PENDING !=  GetLastError())
//		{
//			printf("[Stop] DeleteTimerQueueTimer failed (%d)\n", GetLastError());
//			return false;
//		}
//		//printf("DeleteTimerQueueTimer Elapse %0.2f ms\n", tc.Elapse());
//
//		//tc.Start();
//		if (WAIT_OBJECT_0 != WaitForSingleObject(m_hTimerStop, INFINITE))
//		{
//			printf("[Stop] WaitForSingleObject m_hTimerStop failed (%d)\n", GetLastError());
//			return false;
//		}
//		CloseHandle(m_hTimerStop);
//		m_hTimerStop = NULL;
//		//printf("WaitForSingleObject Elapse %0.2f ms\n", tc.Elapse());
//
//		//tc.Start();
//		if (false == DeleteTimerQueueEx(m_hTimerQueue, m_hTimerQueuestop))
//		{
//			printf("[Stop] DeleteTimerQueueEx failed (%d)\n", GetLastError());
//			return false;
//		}
//		//printf("DeleteTimerQueueEx Elapse %0.2f ms\n", tc.Elapse());
//		
//		//tc.Start();
//		if (WAIT_OBJECT_0 != WaitForSingleObject(m_hTimerQueuestop, INFINITE))
//		{
//			printf("[Stop] WaitForSingleObject m_hTimerQueuestop failed (%d)\n", GetLastError());
//			return false;
//		}
//		CloseHandle(m_hTimerQueuestop);
//		m_hTimerQueuestop = NULL;
//		//printf("WaitForSingleObject Elapse %0.2f ms\n", tc.Elapse());
//
//
//
//	} 
//	else
//	{
//		printf("[Stop]  m_hTimer || m_hTimerStop || m_hTimerQueue || m_hTimerQueuestop NULL\n");
//		return false;
//	}
//
//	return true;
//}




//#################################################################################################################
//#################################################################################################################
//#################################################################################################################






CTimerRoutine::CTimerRoutine()
{
	
	/********************DB Timer****************************************************/
	FireCondition fc_statis[1];

	GetLocalTime(&fc_statis[0].starttm);
	GetLocalTime(&fc_statis[0].endtm);

	fc_statis[0].type = TimerFire_namespace::statisfiretimer;		
	fc_statis[0].starttm.wHour = ST_FIRETIME_0_STARTHOUR;
	fc_statis[0].starttm.wMinute = ST_FIRETIME_0_STARTMIN;
	fc_statis[0].starttm.wSecond = ST_FIRETIME_0_STARTSEC;
	fc_statis[0].endtm.wHour = ST_FIRETIME_0_ENDHOUR;
	fc_statis[0].endtm.wMinute = ST_FIRETIME_0_ENDMIN;
	fc_statis[0].endtm.wSecond = ST_FIRETIME_0_ENDSEC;
	

	m_vfirecd.push_back(fc_statis[0]);
	/************************************************************************/

	/*********************APPREINIT TIMER***************************************************/
	FireCondition fc_appreinit[1];

	GetLocalTime(&fc_appreinit[0].starttm);
	GetLocalTime(&fc_appreinit[0].endtm);

	fc_appreinit[0].type = TimerFire_namespace::appreinitfiretimer;		
	fc_appreinit[0].starttm.wHour = APPREINIT_FIRETIME_0_STARTHOUR;
	fc_appreinit[0].starttm.wMinute = APPREINIT_FIRETIME_0_STARTMIN;
	fc_appreinit[0].starttm.wSecond = APPREINIT_FIRETIME_0_STARTSEC;
	fc_appreinit[0].endtm.wHour = APPREINIT_FIRETIME_0_ENDHOUR;
	fc_appreinit[0].endtm.wMinute = APPREINIT_FIRETIME_0_ENDMIN;
	fc_appreinit[0].endtm.wSecond = APPREINIT_FIRETIME_0_ENDSEC;



	m_vfirecd.push_back(fc_appreinit[0]);
	/************************************************************************/


}

CTimerRoutine::~CTimerRoutine(){}
//


bool CTimerRoutine::TimerFire(TimerFire_namespace::TimerFireType type)
{
	

	unsigned int size = m_vfirecd.size();

	for (unsigned int i = 0; i < size; i++)
	{
		//only call once one day, judge from date
		if (type == m_vfirecd[i].type && false == m_vfirecd[i].TodayFired())
		{
			SYSTEMTIME curtime;
			GetLocalTime(&curtime);

			//for across day, convert into the same day
			SetFireConditionDate(curtime, m_vfirecd[i].starttm);
			SetFireConditionDate(curtime, m_vfirecd[i].endtm);

			CTime cur_ctime(curtime);
			CTime start_ctime(m_vfirecd[i].starttm);
			CTime end_ctime(m_vfirecd[i].endtm);
			CTimeSpan startspan = cur_ctime - start_ctime;
			CTimeSpan endspan = end_ctime - cur_ctime;


			if (startspan.GetTotalSeconds() >= 0 && endspan.GetTotalSeconds() >= 0)
			{

				SetFireConditionDate(curtime, m_vfirecd[i].firedata);

				return true;
			}
		}

	}


	return false;

}






void CTimerRoutine::SetFireConditionDate(SYSTEMTIME& sourcetm, SYSTEMTIME& desttm)
{
	desttm.wYear = sourcetm.wYear;
	desttm.wMonth = sourcetm.wMonth;
	desttm.wDay = sourcetm.wDay;
	//desttm.wDayOfWeek = sourcetm.wDayOfWeek;
	//desttm.wHour = sourcetm.wHour;
	//desttm.wMinute = sourcetm.wMinute;
	//desttm.wSecond = sourcetm.wSecond;
	desttm.wMilliseconds = sourcetm.wMilliseconds;

}



//
//
//
//CTimerAndCTimerRoutine::CTimerAndCTimerRoutine(unsigned int interval,   // interval in ms
//	unsigned int duetime,// true to call first event immediately
//	bool once)       // true to call timed event only once
//{
//	m_pctimer = new CTimer(interval, duetime, once);
//	if (NULL == m_pctimer)
//	{
//		printf("[CTimerAndCTimerRoutine] m_pctimer NULL\n");
//	}
//
//	m_pctimer_routine = new CTimerRoutine();
//	if (NULL == m_pctimer_routine)
//	{
//		printf("[CTimerAndCTimerRoutine] m_pctimer_routine NULL\n");
//	}
//}
//
//CTimerAndCTimerRoutine::~CTimerAndCTimerRoutine()
//{
//	if (m_pctimer)
//	{
//		delete m_pctimer;
//		m_pctimer = NULL;
//	}
//
//
//	if (m_pctimer_routine)
//	{
//		delete m_pctimer_routine;
//		m_pctimer_routine = NULL;
//	}
//
//
//}
//
//
//
//bool CTimerAndCTimerRoutine::TimerFire(TimerFire_namespace::TimerFireType type)
//{
//	if (m_pctimer_routine)
//	{
//		return m_pctimer_routine->TimerFire(type);
//	} 
//
//	return false;
//}
//
